import { getDevInfo, getDevDetail, getCityList } from '../services/api';

const randomPosition = array => ({
  longitude: array[0] + 0.05 - Math.random() * 0.1,
  latitude: array[1] + 0.05 - Math.random() * 0.1,
});

export default {
  namespace: 'monitor',

  state: {
    tags: [],
    devNum: 0,
    onDevNum: 0,
    weekOffDevNum: 0,
    yearOffDevNum: 0,
    progDevNum: 0,
    vehCount: 0,
    todayVehCount: 0,
    devinfo: {},
    devList: [],
    cityList: [],
  },

  effects: {
    *fetchTags({ payload }, { call, put }) {
      //console.log('payload', payload);
      const response = yield call(getDevInfo, payload);
      if (response) {
        const markerarray = response.devInfos
          ? response.devInfos
            .filter(item => item.gpsPos.lng !== null)
            .map(item => {
              // if (item.gpsPos.lng) {
              return {
                position: { lng: parseFloat(item.gpsPos.lng), lat: parseFloat(item.gpsPos.lat) },
                ...item,
              };
              //
              // } else {
              //   return { position: randomPosition(payload.center), ...item };
              // }
            })
          : [];
        const { devNum, onDevNum, weekOffDevNum, yearOffDevNum, vehCount, todayVehCount, progDevNum } = response;
        yield put({
          type: 'saveTags',
          payload: { devNum, onDevNum, weekOffDevNum, yearOffDevNum, markerarray, vehCount, todayVehCount, progDevNum },
        });
      }
    },
    *fetchDevList({ payload }, { call, put }) {
      const response = yield call(getDevInfo, payload);
      if (response) {
        const markerarray = response.devInfos ? response.devInfos : []; // .filter(item => Object.keys(item.gpsPos).length !== 0)
        yield put({
          type: 'saveDevList',
          payload: markerarray,
        });
      }
    },
    *fetchCity({ payload, callback }, { call, put }) {
      const response = yield call(getCityList, payload);
      if (response) {
        const cityarray = response.cityCodes ? response.cityCodes : []; // .filter(item => Object.keys(item.gpsPos).length !== 0)
        yield put({
          type: 'saveCityList',
          payload: cityarray,
        });
        if (cityarray.length > 0) callback(cityarray);
      }
    },
    *fetchmarkerinfo({ payload }, { call, put }) {
      // console.log('payload', payload);
      const response = yield call(getDevDetail, payload);
      if (response) {
        yield put({
          type: 'updateDevInfo',
          payload: response.devInfoDet,
        });
      }
    },
    // *getDevList({ payload }, { call, put }) {
    //   const response = yield call(getDevList, payload);
    //   //console.log('respones', response);
    //   if (response) {
    //     yield put({
    //       type: 'devList',
    //       payload: response.devList,
    //     });
    //   }
    // },
  },

  reducers: {
    saveTags(state, action) {
      return {
        ...state,
        tags: action.payload.markerarray,
        devNum: action.payload.devNum,
        onDevNum: action.payload.onDevNum,
        weekOffDevNum: action.payload.weekOffDevNum,
        yearOffDevNum: action.payload.yearOffDevNum,
        progDevNum: action.payload.progDevNum,
        vehCount: action.payload.vehCount,
        todayVehCount: action.payload.todayVehCount,
      };
    },
    // updateStatus(state, action) {
    //   return {
    //     ...state,
    //     DevNum: action.payload.DevNum,
    //     onDevNum: action.payload.onDevNum,
    //     weekOffDevNum: action.payload.weekOffDevNum,
    //     yearOffDevNum: action.payload.yearOffDevNum,
    //   };
    // },
    updateDevInfo(state, action) {
      return {
        ...state,
        devinfo: action.payload,
      };
    },
    saveDevList(state, action) {
      return {
        ...state,
        devList: action.payload,
      };
    },
    saveCityList(state, action) {
      return {
        ...state,
        cityList: action.payload,
      };
    },
    // devList(state, action) {
    //   return {
    //     ...state,
    //     devList: action.payload,
    //   };
    // },
  },
};
